import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:qaun_ni_dev/utils/request.dart';
import 'package:qaun_ni_dev/model/appUser_model.dart';
import 'package:qaun_ni_dev/utils/toast.dart';

class AppUserProvide with ChangeNotifier {

  int page = 1;
  var currentPage;
  var infoStatu;
  var nickName;
  var phone;
  var sex;
  var accountStatu;


  List<AppUserData> appUserDataList = [];




  // 获取改变infoStatu的值
  changeInfoState(value) {
    infoStatu = value;
    notifyListeners();
  }
  // 获取改变accountStatu的值
  changeStateValue(value) {
    accountStatu = value;
    notifyListeners();
  }
  // 获取改变sex的值
  changeSexValue(value) {
    sex = value;
    notifyListeners();
  }





  //  获取app端用户列表
  getAppUserDataList (context)async {
    var formData = {
      'currentPage': page,
      'infoStatu': infoStatu,
      'nickName': nickName,
      'phone': phone,
      'sex': sex,
      'accountStatu': accountStatu
    };

    await requestPost(context,'/appUser/list', formData: formData).then((value) {
      var response = json.decode(value.toString());
//      print(response);
      var tempData = AppUserModel.fromJson(response);
      appUserDataList = tempData.data;
      notifyListeners();
    });
  }

  // 下拉加载更多
  getAppUserMoreList (context) async {
    page++;
    print('页码${page}');
    var formData = {
      'currentPage': page,
      'infoStatu': infoStatu,
      'nickName': nickName,
      'phone': phone,
      'sex': sex,
      'accountStatu': accountStatu
    };


    await requestPost(context,'/appUser/list',formData: formData).then((value) {
      var responseData = json.decode(value.toString());
      print('aaa>>>>>>>${responseData['data'].length >0}');
      if(responseData['data'].length >0) {
        var tempData = AppUserModel.fromJson(responseData);
        var tempDatas = tempData.data;
        appUserDataList.addAll(tempDatas);
        print('长度${appUserDataList.length}');
      } else {
        Toast.toast(context,msg: '已经没有数据了哦');
      }

      notifyListeners();
    });
  }

  // 上拉刷新
  getOnRefreshList (context) async{
    page = 1;
    appUserDataList= [];
    var formData = {
      'currentPage': page,
      'infoStatu': infoStatu,
      'nickName': nickName,
      'phone': phone,
      'sex': sex,
      'accountStatu': accountStatu
    };
    await requestPost(context,'/appUser/list', formData: formData).then((value) {
      var response = json.decode(value.toString());
      print(response);
      var tempData = AppUserModel.fromJson(response);
      appUserDataList = tempData.data;
      notifyListeners();
    });
  }
  
  // 封禁用户
  prohibitUser (context,userId) async {
    var formData = {
      'accountStatu': 1,
      'uId':userId
    };
    await requestPost(context,'/appUser/updateAccountStatu',formData: formData).then((value){
      var res = json.decode(value.toString());
      print('封禁用户${res}');
      if(res['code'] == 200) {
        Toast.toast(context,msg: '已封禁用户');
        Navigator.pushNamed(context, '/AppUserPages');
      }
    });
  }

  // 解封用户
  unsealUser (context,userId) async {
    var formData = {
      'accountStatu': 0,
      'uId':userId
    };
    await requestPost(context,'/appUser/updateAccountStatu',formData: formData).then((value){
      var res = json.decode(value.toString());
      if(res['code'] == 200) {
        Toast.toast(context,msg: '已成功解封用户');
        Navigator.pushNamed(context, '/AppUserPages');
      }
    });
  }

}



